package com.lzp.aop.aspect;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

/**
 * @author : lzp
 * @version 1.0
 * @date : 2022/2/26 15:54
 * @apiNote : TODO
 */
@Aspect
@Component
@Slf4j
public class AspectB {
    /**
     * 结论:within只对类起作用,接口中的默认实现也可以拦截
     * annotation只对类的方法有作用,接口的默认方法也行
     */
    @Pointcut("@annotation(com.lzp.aop.antion.MyAOP)||@within(com.lzp.aop.antion.MyAOP)")
    public void method() {
    }

    @SneakyThrows
    @Around(value = "method()")
    public Object tokenCheckHandler(ProceedingJoinPoint proceedingJoinPoint) {
        StopWatch stopWatch = new StopWatch();
        Object[] args = proceedingJoinPoint.getArgs();
        for (Object arg : args) {
            System.out.println("这是切面的参数:"+arg);
        }
        try {
            stopWatch.start();
            return proceedingJoinPoint.proceed();
        }catch (NullPointerException e){
            log.warn("sql查询出现错误:{}",e.getMessage());
        }finally {
            stopWatch.stop();
            long totalTimeMillis = stopWatch.getTotalTimeMillis();
            log.info("查询耗时:{}毫秒",totalTimeMillis);
        }
        return null;
    }
}
